{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas import DataFrame,Series\n",
    "import pandas as pd\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[34, 85, 75, 65, 86, 79],\n       [96, 88, 61, 62, 88, 63],\n       [38, 74, 62, 85, 20,  4],\n       [28, 97,  5, 96, 89,  3],\n       [65, 90, 89, 55, 17, 88],\n       [21, 76, 14, 21, 56, 84]])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "index = [\n",
    "    ['1班', '1班', '1班', '2班', '2班', '2班'],\n",
    "    ['张三', '李四', '王五', '赵六', '田七', '孙八']\n",
    "]\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中          期末        \n       语文  数学  英语  语文  数学  英语\n1班 张三  34  85  75  65  86  79\n   李四  96  88  61  62  88  63\n   王五  38  74  62  85  20   4\n2班 赵六  28  97   5  96  89   3\n   田七  65  90  89  55  17  88\n   孙八  21  76  14  21  56  84",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">期中</th>\n      <th colspan=\"3\" halign=\"left\">期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">1班</th>\n      <th>张三</th>\n      <td>34</td>\n      <td>85</td>\n      <td>75</td>\n      <td>65</td>\n      <td>86</td>\n      <td>79</td>\n    </tr>\n    <tr>\n      <th>李四</th>\n      <td>96</td>\n      <td>88</td>\n      <td>61</td>\n      <td>62</td>\n      <td>88</td>\n      <td>63</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>38</td>\n      <td>74</td>\n      <td>62</td>\n      <td>85</td>\n      <td>20</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">2班</th>\n      <th>赵六</th>\n      <td>28</td>\n      <td>97</td>\n      <td>5</td>\n      <td>96</td>\n      <td>89</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>田七</th>\n      <td>65</td>\n      <td>90</td>\n      <td>89</td>\n      <td>55</td>\n      <td>17</td>\n      <td>88</td>\n    </tr>\n    <tr>\n      <th>孙八</th>\n      <td>21</td>\n      <td>76</td>\n      <td>14</td>\n      <td>21</td>\n      <td>56</td>\n      <td>84</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = DataFrame(\n",
    "\tdata = data,\n",
    "\tindex = index,\n",
    "\tcolumns= columns\n",
    ")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "1班  张三    34\n    李四    96\n    王五    38\n2班  赵六    28\n    田七    65\n    孙八    21\nName: 语文, dtype: int32"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=6)\n",
    "index = [\n",
    "    ['1班', '1班', '1班', '2班', '2班', '2班'],\n",
    "    ['张三', '李四', '王五', '赵六', '田七', '孙八']\n",
    "]\n",
    "s  = pd.Series(data=data, index=index)\n",
    "s\n",
    "\n",
    "df.期中.语文"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中          期末        \n       语文  数学  英语  语文  数学  英语\n1班 张三  17  44  98  86  54  54\n   李四  77  31   1  27  95  25\n   王五  91   9  58  81  88  64\n2班 赵六  87  37  91  33  31  23\n   田七  87  74   7  33  19  27\n   孙八  15  57  23  24  51  11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">期中</th>\n      <th colspan=\"3\" halign=\"left\">期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">1班</th>\n      <th>张三</th>\n      <td>17</td>\n      <td>44</td>\n      <td>98</td>\n      <td>86</td>\n      <td>54</td>\n      <td>54</td>\n    </tr>\n    <tr>\n      <th>李四</th>\n      <td>77</td>\n      <td>31</td>\n      <td>1</td>\n      <td>27</td>\n      <td>95</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>91</td>\n      <td>9</td>\n      <td>58</td>\n      <td>81</td>\n      <td>88</td>\n      <td>64</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">2班</th>\n      <th>赵六</th>\n      <td>87</td>\n      <td>37</td>\n      <td>91</td>\n      <td>33</td>\n      <td>31</td>\n      <td>23</td>\n    </tr>\n    <tr>\n      <th>田七</th>\n      <td>87</td>\n      <td>74</td>\n      <td>7</td>\n      <td>33</td>\n      <td>19</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>孙八</th>\n      <td>15</td>\n      <td>57</td>\n      <td>23</td>\n      <td>24</td>\n      <td>51</td>\n      <td>11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "index = pd.MultiIndex.from_arrays( [\n",
    "    ['1班', '1班', '1班', '2班', '2班', '2班'],\n",
    "    ['张三', '李四', '王五', '赵六', '田七', '孙八']\n",
    "])\n",
    "\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "df = DataFrame(data=data,index=index,columns=columns)\n",
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中          期末        \n       语文  数学  英语  语文  数学  英语\n1班 张三  88  76  61  54  85  20\n   李四  52  39  40  59   8  56\n   王五  32  55  42  99   0  11\n2班 赵六  47  56  24  18   4  51\n   田七  10  10  25  44  76  96\n   孙八  89  19  21  52  88  46",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">期中</th>\n      <th colspan=\"3\" halign=\"left\">期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">1班</th>\n      <th>张三</th>\n      <td>88</td>\n      <td>76</td>\n      <td>61</td>\n      <td>54</td>\n      <td>85</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>李四</th>\n      <td>52</td>\n      <td>39</td>\n      <td>40</td>\n      <td>59</td>\n      <td>8</td>\n      <td>56</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>32</td>\n      <td>55</td>\n      <td>42</td>\n      <td>99</td>\n      <td>0</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">2班</th>\n      <th>赵六</th>\n      <td>47</td>\n      <td>56</td>\n      <td>24</td>\n      <td>18</td>\n      <td>4</td>\n      <td>51</td>\n    </tr>\n    <tr>\n      <th>田七</th>\n      <td>10</td>\n      <td>10</td>\n      <td>25</td>\n      <td>44</td>\n      <td>76</td>\n      <td>96</td>\n    </tr>\n    <tr>\n      <th>孙八</th>\n      <td>89</td>\n      <td>19</td>\n      <td>21</td>\n      <td>52</td>\n      <td>88</td>\n      <td>46</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "index = pd.MultiIndex.from_tuples(\n",
    "    (\n",
    "        ('1班', '张三'), ('1班', '李四'), ('1班', '王五'),\n",
    "        ('2班', '赵六'), ('2班', '田七'), ('2班', '孙八'),\n",
    "    )\n",
    ")\n",
    "\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中          期末        \n       语文  数学  英语  语文  数学  英语\n1班 张三  60  33  78  65  88  78\n   李四  74  65   4  64  68  74\n   王五  11  19  31  11  57   1\n2班 张三  29  86  12  32  85  95\n   李四  25  17  84  49  64  28\n   王五  23  96  24  88  25  70",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">期中</th>\n      <th colspan=\"3\" halign=\"left\">期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">1班</th>\n      <th>张三</th>\n      <td>60</td>\n      <td>33</td>\n      <td>78</td>\n      <td>65</td>\n      <td>88</td>\n      <td>78</td>\n    </tr>\n    <tr>\n      <th>李四</th>\n      <td>74</td>\n      <td>65</td>\n      <td>4</td>\n      <td>64</td>\n      <td>68</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>11</td>\n      <td>19</td>\n      <td>31</td>\n      <td>11</td>\n      <td>57</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">2班</th>\n      <th>张三</th>\n      <td>29</td>\n      <td>86</td>\n      <td>12</td>\n      <td>32</td>\n      <td>85</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>李四</th>\n      <td>25</td>\n      <td>17</td>\n      <td>84</td>\n      <td>49</td>\n      <td>64</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>23</td>\n      <td>96</td>\n      <td>24</td>\n      <td>88</td>\n      <td>25</td>\n      <td>70</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "index = pd.MultiIndex.from_product(\n",
    "    [\n",
    "        ['1班',  '2班'],\n",
    "        ['张三', '李四', '王五']\n",
    "    ]\n",
    ")\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "1班  张三    57\n    李四     1\n    王五    66\n2班  赵六    90\n    田七    86\n    孙八    98\ndtype: int32"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series多层次索引\n",
    "data = np.random.randint(0, 100, size=6)\n",
    "index = [\n",
    "    ['1班', '1班', '1班', '2班', '2班', '2班'],\n",
    "    ['张三', '李四', '王五', '赵六', '田七', '孙八']\n",
    "]\n",
    "s  = pd.Series(data=data, index=index)\n",
    "s\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "1班  李四     1\n    王五    66\ndtype: int32"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[0]\n",
    "s.iloc[1]\n",
    "s.iloc[[1,2]]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "张三    57\n李四     1\n王五    66\ndtype: int32"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[\"1班\"]\n",
    "s.loc[\"1班\"]\n",
    "s.loc[\"1班\",\"张三\"]\n",
    "s.loc[(\"1班\",\"张三\")]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "Series([], dtype: int32)"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "s[\"1班\":\"2班\"]\n",
    "s.loc['1班': '2班']\n",
    "# s.loc['李四': '赵六']\n",
    "# s[(\"1班\",\"李四\"):(\"2班\",\"赵六\")]# 报错"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "1班  李四     1\n    王五    66\n2班  赵六    90\ndtype: int32"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.iloc[1:4]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中          期末        \n       语文  数学  英语  语文  数学  英语\n1班 张三  89  33  74  57  23   8\n   李四  15  24  54  16  68  78\n   王五  62  51  66  88  51  64\n2班 张三  11  66  43   0  30  78\n   李四  43  30  61  66  59  26\n   王五   9   6  90  74  17  74",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">期中</th>\n      <th colspan=\"3\" halign=\"left\">期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">1班</th>\n      <th>张三</th>\n      <td>89</td>\n      <td>33</td>\n      <td>74</td>\n      <td>57</td>\n      <td>23</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>李四</th>\n      <td>15</td>\n      <td>24</td>\n      <td>54</td>\n      <td>16</td>\n      <td>68</td>\n      <td>78</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>62</td>\n      <td>51</td>\n      <td>66</td>\n      <td>88</td>\n      <td>51</td>\n      <td>64</td>\n    </tr>\n    <tr>\n      <th rowspan=\"3\" valign=\"top\">2班</th>\n      <th>张三</th>\n      <td>11</td>\n      <td>66</td>\n      <td>43</td>\n      <td>0</td>\n      <td>30</td>\n      <td>78</td>\n    </tr>\n    <tr>\n      <th>李四</th>\n      <td>43</td>\n      <td>30</td>\n      <td>61</td>\n      <td>66</td>\n      <td>59</td>\n      <td>26</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>9</td>\n      <td>6</td>\n      <td>90</td>\n      <td>74</td>\n      <td>17</td>\n      <td>74</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "index = pd.MultiIndex.from_product(\n",
    "    [\n",
    "        ['1班',  '2班'],\n",
    "        ['张三', '李四', '王五']\n",
    "    ]\n",
    ")\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "\n",
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "data": {
      "text/plain": "期中  语文    89\n    数学    33\n    英语    74\n期末  语文    57\n    数学    23\n    英语     8\nName: (1班, 张三), dtype: int32"
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先列\n",
    "df.期中\n",
    "df[\"期中\"]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中          期末       \n       语文  数学  英语  语文  数学 英语\n1班 张三  89  33  74  57  23  8",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">期中</th>\n      <th colspan=\"3\" halign=\"left\">期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1班</th>\n      <th>张三</th>\n      <td>89</td>\n      <td>33</td>\n      <td>74</td>\n      <td>57</td>\n      <td>23</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取行\n",
    "df.loc[\"1班\"]\n",
    "df.iloc[[0]]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "54"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取数据\n",
    "df.iloc[1][2]\n",
    "df.iloc[1,2]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "data": {
      "text/plain": "54"
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[(\"1班\",\"李四\")].loc[(\"期中\",\"英语\")]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "54"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[(\"1班\",\"李四\"),(\"期中\",\"英语\")]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中          期末        \n       语文  数学  英语  语文  数学  英语\n1班 李四  15  24  54  16  68  78\n   王五  62  51  66  88  51  64\n2班 张三  11  66  43   0  30  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th colspan=\"3\" halign=\"left\">期中</th>\n      <th colspan=\"3\" halign=\"left\">期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">1班</th>\n      <th>李四</th>\n      <td>15</td>\n      <td>24</td>\n      <td>54</td>\n      <td>16</td>\n      <td>68</td>\n      <td>78</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>62</td>\n      <td>51</td>\n      <td>66</td>\n      <td>88</td>\n      <td>51</td>\n      <td>64</td>\n    </tr>\n    <tr>\n      <th>2班</th>\n      <th>张三</th>\n      <td>11</td>\n      <td>66</td>\n      <td>43</td>\n      <td>0</td>\n      <td>30</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[[1,2,3]]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "       期中  期末\n       英语  语文\n1班 李四  54  16\n   王五  66  88",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>期中</th>\n      <th>期末</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th></th>\n      <th>英语</th>\n      <th>语文</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">1班</th>\n      <th>李四</th>\n      <td>54</td>\n      <td>16</td>\n    </tr>\n    <tr>\n      <th>王五</th>\n      <td>66</td>\n      <td>88</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[[(\"期中\",\"英语\"),(\"期末\",\"语文\")]].loc[[(\"1班\",\"李四\"),(\"1班\",\"王五\")]]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}